﻿using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Xml;
using AvalonDock;
using DCT.ViewModel;

namespace DCT.View
{
    public abstract class DeviceBaseView : MoveThumb
    {
        #region Constructor

        public DeviceBaseView()
        {
            this.DataContextChanged += DeviceView_DataContextChanged;
            this.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(DeviceBaseView_PreviewMouseLeftButtonDown);
        }

        static DeviceBaseView()
        {
            CustomPropertyConfigXML = new XmlDocument();
            try
            {
                CustomPropertyConfigXML.Load(@".\PropertyWindow\CustomPropertyConfig.xml");
            }
            catch
            {

            }
        }

        #endregion // Constructor

        #region Fields

        DeviceBaseViewModel _deviceViewModel;
        protected DeviceVisualHost _deviceVisual;
        protected string _visualXMLFile;        

        #endregion // Fields

        #region Properties

        public static XmlDocument CustomPropertyConfigXML { get; private set; }

        protected virtual Drawing visualDrawing { get; set; }

        public DeviceBaseViewModel DeviceViewModel
        {
            get
            {
                return _deviceViewModel;
            }
        }

        public DeviceVisualHost DeviceVisual
        {
            get
            {
                return _deviceVisual;
            }
            set
            {
                _deviceVisual = value;
            }
        }

        public string VisualXMLFile
        {
            get
            {
                return (string)GetValue(VisualXMLFileProperty);
            }
            set
            {
                SetValue(VisualXMLFileProperty, value);
            }
        }

        public static DependencyProperty VisualXMLFileProperty = DependencyProperty.Register("VisualXMLFile", typeof(string), typeof(DeviceBaseView), new PropertyMetadata("", VisualXMLFilePropertyChanged));

        static void VisualXMLFilePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DeviceBaseView deviceView = d as DeviceBaseView;

            if (deviceView.visualDrawing == null)
            {
                XmlReader xmlReader = XmlReader.Create((string)e.NewValue);
                deviceView.visualDrawing = (Drawing)XamlReader.Load(xmlReader);
                deviceView.visualDrawing.Freeze();
            }           
        }

        #endregion //Properties

        #region Methods

        public virtual void UpdateVisual()
        {
            if (visualDrawing != null)
            {
                Drawing visualDrawingClone = visualDrawing.CloneCurrentValue();
                visualDrawingClone.Freeze();

                DrawingVisual drawingVisual = new DrawingVisual();
                DrawingContext drawingContext = drawingVisual.RenderOpen();
                drawingContext.DrawDrawing(visualDrawingClone);
                drawingContext.Close();

                _deviceVisual.Children.Add(drawingVisual);
            }
        }

        protected virtual void DeviceView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext is DeviceBaseViewModel)
            {
                _deviceViewModel = e.NewValue as DeviceBaseViewModel;
                _deviceViewModel.PropertyChanged += DevicePropertyChanged;
            }
            else
            {
                //删除设备时(DisconnectedItem)
                if (_deviceViewModel != null)
                {
                    _deviceViewModel.PropertyChanged -= DevicePropertyChanged;
                }
            }
        }

        void DevicePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsPropertyShow" && _deviceViewModel.IsPropertyShow)
            {
                if (DeviceBaseView.CustomPropertyConfigXML == null)
                {
                    return;
                }

                DockableContent propertyWindow = new DockableContent();

                PropertyWindowView propertyWindowView = new PropertyWindowView();
                propertyWindowView.DataContext = _deviceViewModel;

                XmlNode tmpXNode = DeviceBaseView.CustomPropertyConfigXML.SelectSingleNode("Components/Component[@Name=\"" + _deviceViewModel.DeviceType.ToString() + "\"]");
                XmlNodeList tmpXPropLst = tmpXNode.SelectNodes("Propertys/Property");
                CustomProperty customProperty = new CustomProperty(_deviceViewModel, tmpXPropLst);
                propertyWindowView.PropertyGrid.SelectedObject = customProperty;

                propertyWindow.Content = propertyWindowView;
                propertyWindow.Title = _deviceViewModel.DeviceName;
                propertyWindow.Closed += propertyWindowView.PropertyWindowClosed;

                propertyWindow.Show(MainWindowView.DockManager, AnchorStyle.Right);
            }
        }

        void DeviceBaseView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (_deviceViewModel.DeviceMapViewModel != null && !(_deviceViewModel.DeviceMapViewModel.EditMode == EditModeState.DrawSwitch || _deviceViewModel.DeviceMapViewModel.EditMode == EditModeState.DrawTrackSegment))
            {
                DeviceCanvas deviceCanvas = Util.GetParentVisual<DeviceCanvas>(this);

                if (deviceCanvas != null)
                {
                    if ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) != ModifierKeys.None)
                    {
                        this._deviceViewModel.IsSelected = !this._deviceViewModel.IsSelected;
                    }
                    else
                    {
                        deviceCanvas.DeviceMapViewModel.DeselectAll_Execute();
                        this._deviceViewModel.IsSelected = true;
                    }
                }
            }
        }

        #endregion // Methods
    }
}
